﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GUI
{
    public partial class FormDoiMonGoi : Form
    {
        public FormDoiMonGoi()
        {
            InitializeComponent();
        }
        public int NVGoiMon, SoLuong, KhuyenMai;
        public string Loai, Mon;
        bool loai = false;
        public NHANVIENDTO NVDTO;
        public List<LOAIMONDTO> dsl;
        public List<THUCDONDTO> dsm;
        public List<NHANVIENDTO> dsg;
        public LOAIMONDTO LDTO;
        public THUCDONDTO MDTO;
        public BANDTO BDTO;
        public delegate void GetString(String Loai, String Mon, String DVT, bool cMon);
        public GetString CapNhat;
        public void LayDanhSachNhanVienGoiMon()
        {
            int i;
            cboNVGoiMon.Items.Clear();
            dsg = new List<NHANVIENDTO>();
            NHANVIENBUS NVBUS = new NHANVIENBUS();
            dsg = NVBUS.LayDSNVGoiMon();
            for (i = 0; i < dsg.Count; i++)
            {
                cboNVGoiMon.Items.Add(dsg[i].MaNV);
            }
            if (i > 0) cboNVGoiMon.SelectedIndex = 0;
        }
        public void LayDanhSachLoaiMon()
        {
            int i;
            cboLoaiMon.Items.Clear();
            cboLoaiMon.Text = "";
            dsl = new List<LOAIMONDTO>();
            LOAIMONBUS LBUS = new LOAIMONBUS();
            dsl = LBUS.LayDSLoai(BDTO, loai);
            for (i = 0; i < dsl.Count; i++)
            {
                cboLoaiMon.Items.Add(dsl[i].TenLoai);
            }
            if (cboLoaiMon.Items.Count > 0) cboLoaiMon.SelectedIndex = 0;
            else
            {
                cboMon.Items.Clear();
                cboMon.Text = "";
            };
        }
        public void LayDanhSachMon()
        {
            int i;
            cboMon.Items.Clear();
            cboMon.Text = "";
            if (cboLoaiMon.SelectedIndex == -1) return;
            dsm = new List<THUCDONDTO>();
            THUCDONBUS MBUS = new THUCDONBUS();
            dsm = MBUS.LayDSMon(dsl[cboLoaiMon.SelectedIndex], BDTO);
            for (i = 0; i < dsm.Count; i++)
            {
                cboMon.Items.Add(dsm[i].TenMon);
            }
            if (cboMon.Items.Count > 0) cboMon.SelectedIndex = 0;
            else
            {
                txtDVT.Text = "";
            };
        }
        public void LayDVT()
        {
            THUCDONBUS MBUS = new THUCDONBUS();
            txtDVT.Text = MBUS.LayDVT(dsm[cboMon.SelectedIndex]);
        }
        private void FormDoiMonGoi_Load(object sender, EventArgs e)
        {
            this.Text += " - NV: " + NVDTO.MaNV + " " + NVDTO.TenNV;
            //Lấy danh sách nhân viên gọi món
            LayDanhSachNhanVienGoiMon();
            //Lấy danh sách loại món
            LayDanhSachLoaiMon();
            //Lấy danh sách món
            LayDanhSachMon();
            cboNVGoiMon.SelectedItem = NVGoiMon;
            rdMon.Checked = LDTO.Loai;
            if (rdMon.Checked)
            {
                rdNuoc.Checked = false;
            }
            else
            {
                rdNuoc.Checked = true;
            }
            cboLoaiMon.SelectedItem = LDTO.TenLoai;
            for (int i = 0; i < dsm.Count; i++)
            {
                if (dsm[i].TenMon == MDTO.TenMon && dsm[i].DVT == MDTO.DVT)
                {
                    cboMon.SelectedIndex = i;
                }
            }
        }
        private void btnXong_Click(object sender, EventArgs e)
        {
            HOADONDTO HDTO = new HOADONDTO();
            HOADONBUS HBUS = new HOADONBUS();
            CHITIETHOADONDTO CDTOCu = new CHITIETHOADONDTO();
            CHITIETHOADONDTO CDTOMoi = new CHITIETHOADONDTO();
            CHITIETHOADONBUS CBUS = new CHITIETHOADONBUS();
            HDTO = HBUS.LayThongTinHD(BDTO);
            //DOIMON(@MaHD int, @MaNVCu int, @MaNVMoi int, @MaMonCu int, @MaMonMoi int, @SoLuong int)
            CDTOCu.MaHD = HDTO.MaHD;
            CDTOCu.MaNV = NVGoiMon;
            CDTOCu.MaMon = MDTO.MaMon;
            //Moi
            CDTOMoi.MaMon = dsm[cboMon.SelectedIndex].MaMon;
            CDTOMoi.MaNV = dsg[cboNVGoiMon.SelectedIndex].MaNV;
            CDTOMoi.SoLuong = SoLuong;
            CDTOMoi.KhuyenMai = KhuyenMai;

            if (CBUS.CapNhatThongTin(CDTOCu, CDTOMoi) < 1)
            {
                MessageBox.Show("Cập nhật không thành công.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.Close();
            FormQLGoiMon frm = new FormQLGoiMon();
            CapNhat(cboLoaiMon.Text, cboMon.Text, txtDVT.Text, rdMon.Checked);
        }
        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void cboLoaiMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            LayDanhSachMon();
        }
        private void cboMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            LayDVT();
        }
        private void cboLoaiMon_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            if (e.Index == -1) return;
            if (dsl[e.Index].TinhTrang)//Đã gọi
            {
                e.Graphics.DrawString(cboLoaiMon.Items[e.Index].ToString(), cboLoaiMon.Font, System.Drawing.Brushes.Red, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            }
            else
                e.Graphics.DrawString(cboLoaiMon.Items[e.Index].ToString(), cboLoaiMon.Font, System.Drawing.Brushes.Blue, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            e.DrawFocusRectangle();
        }
        private void cboMon_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            if (e.Index == -1) return;
            if (dsm[e.Index].SL == 0 && dsm[e.Index].KM == 0)//Chưa gọi
            {
                e.Graphics.DrawString(cboMon.Items[e.Index].ToString(), cboMon.Font, System.Drawing.Brushes.Blue, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            }
            else 
                e.Graphics.DrawString(cboMon.Items[e.Index].ToString(), cboMon.Font, System.Drawing.Brushes.Red, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            e.DrawFocusRectangle();
        }
        private void cboLoaiMon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnXong_Click(sender, e);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
        private void cboMon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnXong_Click(sender, e);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
        private void cboNVGoiMon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnXong_Click(sender, e);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
        private void rdMon_CheckedChanged(object sender, EventArgs e)
        {
            if (rdMon.Checked)
            {
                loai = false;
            }
            else
            {
                loai = true;
            }
            LayDanhSachLoaiMon();
            //Lấy danh sách món
            LayDanhSachMon();
        }
    }
}
